package org.teamh.connection.json.handler;

import org.teamh.connection.json.event.JsonEventObject;
import org.teamh.game.Start;
import org.teamh.gui.Event;
import org.teamh.gui.ingame.chat.ChatManager;
import org.teamh.logic.datamodel.DatamodelManager;

public class ChatHandler extends JsonMasterHandler {

	public ChatHandler(Start start, DatamodelManager manager) {
		super(start, manager);
	}
	
	public boolean handleMsg(JsonEventObject json) {
		
		String myName = start.getModel().getPlayerName();
		String myTeam = start.getModel().getTeam();
		
		if (json.getProperty().equals("PUBLIC_MESSAGE")) {
//			start.getGui().getIngameScreen().getIngameScreenHUD().getChatManager().displayMessage(json.getSource(), "", json.getNewValue(), ChatManager.PUBLIC);
			start.getGui().addEvent(new Event("chatMessage", json.getSource() +  "#leer#" + json.getNewValue() + "#" + ChatManager.PUBLIC));
			return true;
		} else if (json.getProperty().equals("USER_MESSAGE")) {
			if(json.getNewValue().startsWith("FLOOD DETECTED!")) {
				//TODO handling??
			} else {
				if (!json.getSource().equals(myName)) {
//					start.getGui().getIngameScreen().getIngameScreenHUD().getChatManager().displayMessage(json.getSource(), myName, json.getNewValue(), ChatManager.PRIVATE);
					start.getGui().addEvent(new Event("chatMessage", json.getSource() + "#" + myName + "#" + json.getNewValue() + "#" + ChatManager.PRIVATE));
				}
			}
			return true;
		} else if (json.getProperty().equals("TEAM_MESSAGE")) {
//			start.getGui().getIngameScreen().getIngameScreenHUD().getChatManager().displayMessage(json.getSource(), myTeam, json.getNewValue(), ChatManager.TEAM);
			start.getGui().addEvent(new Event("chatMessage", json.getSource() + "#" + myTeam + "#" + json.getNewValue() + "#" + ChatManager.TEAM));
			return true;
		}
		return false;
	}
}
